﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using EnvDTE80;
using AutoUnitTest.Common;
using GUAPAddin.Common;
using System.Linq;
using EnvDTE;

namespace GUAPAddin
{
    public partial class GraphView : Form
    {
        private readonly DTE2 _applicationObject;
        private Graph _graph;
        private Vertex _selectedVertex;
        private Edge _selectedEdge;

        public GraphView(DTE2 applicationObject)
        {
            InitializeComponent();
            _applicationObject = applicationObject;

        }

        void VertexGridSelectionChanged(object sender, EventArgs e)
        {
            int selectedRowId = (int)_vertexGrid.SelectedRows[0].Cells[0].Value;

            _selectedVertex = (from x in _graph.VertexList
                           where x.Id == selectedRowId
                        select x).ToList().First();

            var edges = from edg in _graph.EdgeList
                           where (edg.StartVertex == _selectedVertex) || (edg.EndVertex == _selectedVertex)
                           select new
                           {
                               edg.Id,
                               Type = edg.Type.Name,
                               StartVertex = edg.StartVertex.Name,
                               EndVertex = edg.EndVertex.Name,
                           };
            _infoGrid.DataSource = edges.ToList();
        }

        private void BuildGraphClick(object sender, EventArgs e)
        {
        //    try
          //  {
                AnalyzeProcess process = new AnalyzeProcess();
                process.StartAnalyze(_applicationObject);
                _graph = process.getGraph();
        /*    }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + @"\n" + ex.StackTrace + @"\n" + ex.Source);
            }*/
        }

        private void ShowVertexesClick(object sender, EventArgs e)
        {
            var vertexes = from v in _graph.VertexList
                           select new {ID=v.Id, Name=v.Name, Type=v.Type.Name };
            _vertexGrid.DataSource = vertexes.ToList();
            _vertexGrid.SelectionChanged += VertexGridSelectionChanged;
        }

        private void ShowEdgesClick(object sender, EventArgs e)
        {
            var edges = from edg in _graph.EdgeList
                        select new
                        {
                            Id = edg.Id,
                            Type = edg.Type.Name,
                            StartVertex = edg.StartVertex.Name,
                            EndVertex = edg.EndVertex.Name
                        };
            _edgeGrid.DataSource = edges.ToList();
            _edgeGrid.SelectionChanged += EdgeGridSelectionChanged;
        }

        void EdgeGridSelectionChanged(object sender, EventArgs e)
        {
            int selectedRowId = (int)_edgeGrid.SelectedRows[0].Cells[0].Value;

            _selectedEdge = (from x in _graph.EdgeList
                               where x.Id == selectedRowId
                               select x).ToList().First();

            List<Vertex> vxs = new List<Vertex>();

            vxs.Add(_selectedEdge.StartVertex);
            vxs.Add(_selectedEdge.EndVertex);

            _infoGrid.DataSource = vxs;
        }
    }
}
